package hbt;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public abstract class AbstractHibernateManager {

	protected static SessionFactory sessionFactory= null;
	static Session session= null;
	static{
		try {
			sessionFactory= HibernateUtil.getSessionFactory();
			System.out.println("SESSION OK");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public Session getNewSession(){
		Session session= null;
		try {
			session= sessionFactory.openSession();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return session;
	}
	
	public List findAll (String nombre){
		List lista= null;
		Session s= null;
		try {
			s=getNewSession();
			String hql= "FROM "+nombre;
			lista=s.createQuery(hql).list();
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			closeSession(s);
		}
		return lista;
	}
	
	
	public void closeSession(Session s){
		if (s != null){
			s.flush();
			s.close();
		}
	}
}
